package com.example.ncov_management_system.service;

import com.example.ncov_management_system.dao.StudentDao;
import com.example.ncov_management_system.entity.Clbum;
import com.example.ncov_management_system.entity.Login;
import com.example.ncov_management_system.entity.Record;
import com.example.ncov_management_system.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.*;
import javax.persistence.metamodel.Bindable;
import java.util.List;

@Service
@Slf4j
public class StudentService {
    @Autowired
    private StudentDao studentDao;
    @Autowired
    private LoginService loginService;

    public Page<Student> list(int pn, String name, String clbum) {
        return studentDao.findAll(new Specification<Student>() {
            @Override
            public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> uname1 = root.get("name");
                Path<Clbum> clbum1 = root.get("clbum");
                Path<String> clbumName = clbum1.get("name");

                Predicate predicate1 = criteriaBuilder.equal(clbumName, clbum);
                Predicate predicate2 = criteriaBuilder.like(uname1, "%"+name+"%");
                if (StringUtils.isEmpty(name) && StringUtils.isEmpty(clbum)) {
                    return null;
                }
                if (StringUtils.isEmpty(clbum) && !StringUtils.isEmpty(name) ) {
                    return predicate2;
                }
                if (!StringUtils.isEmpty(clbum) && StringUtils.isEmpty(name) ) {
                    return predicate1;
                }
                return criteriaBuilder.and(predicate1, predicate2);
            }
        }, PageRequest.of(pn, 10));
    }

    @Transactional
    public void add(Student student) {
        student = studentDao.save(student);
        Login login = new Login();
        login.setUsername(student.getId() + "");
        login.setRole("学生");
        login.setPassword("123456");
        loginService.add(login);
    }

    public void update(Student student) {
        studentDao.save(student);
    }

    @Transactional
    public void delete(long id) {
        loginService.deleteByUsername(id + "");
        studentDao.deleteById(id);
    }

    public Student findById(long id) {
        return studentDao.findById(id).orElse(null);
    }

    public List<Student> findByClbumId(long clbumId) {
        return studentDao.findAll(new Specification<Student>() {
            @Override
            public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Clbum> clbum = root.get("clbum");
                Path<Long> id = clbum.get("id");
                return criteriaBuilder.equal(id,clbumId);
            }
        });
    }
    public List<Student> list() {
        return studentDao.findAll();
    }
}
